FROM centos:7

MAINTAINER le5le.com

ENV LANG=en_US.UTF-8
ENV TIME_ZONE Asia/Shanghai

RUN ln -snf /usr/share/zoneinfo/$TIME_ZONE /etc/localtime && echo $TIME_ZONE > /etc/timezone

ADD postgresql-15.3.tar.gz /tmp

RUN yum install -y gcc make
RUN yum install -y readline-devel zlib-devel openssl openssl-devel pam pam-devel systemd-devel libxml2 libxml2-devel libxslt libxslt-devel

RUN cd /tmp \
    tar -zxf postgresql-15.3.tar.gz \
    && cd postgresql-15.3 \
    && ./configure --prefix=/usr/pgsql-15 --with-openssl --with-pam --with-systemd --with-libxml --with-libxslt \
    && make world \
    && make install-world

RUN useradd -u 26 postgres \
    && echo "export PGHOME=/usr/pgsql-15" >> /home/postgres/.bash_profile \
    && echo "export PATH=\$PATH:\$PGHOME/bin" >> /home/postgres/.bash_profile \
    && echo "export PGDATA=/var/lib/pgsql/15/data" >> /home/postgres/.bash_profile

ENV PGROOT=/var/lib/pgsql
ENV PGDATA=$PGROOT/15/data

RUN chown -R postgres:postgres "/usr/pgsql-15" \
    && mkdir -p "$PGDATA" \
    && chown -R postgres:postgres "$PGROOT" \
    && chmod -R 700 "$PGROOT"

ENV PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/pgsql-15/bin
RUN su -l postgres -c "initdb --pgdata='$PGDATA' --encoding=UTF8"

EXPOSE 5432

CMD ["postgres"]

# 构建镜像
# docker build -t le5le/postgres:15.3 .

# 运行容器
# docker run --name dbServer -p 5432:5432 -u postgres \
#            -v /var/lib/pgsql/15/data/pg_hba.conf:/var/lib/pgsql/15/data/pg_hba.conf \
#            -v /var/lib/pgsql/15/data/postgresql.conf:/var/lib/pgsql/15/data/postgresql.conf \
#            le5le/postgres:15.3

# 执行命令
# docker exec -it dbServer /bin/bash

